//
//  TKEditPersonalInfoViewController.m
//  Ticket
//
//  Created by 小禹宝宝 on 2017/10/17.
//  Copyright © 2017年 cy55. All rights reserved.
//

#import "TKEditPersonalInfoViewController.h"

#import "NSString+JCKit.h"
#define MAX_LIMIT_NUMS 15
@interface TKEditPersonalInfoViewController ()
<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *verificationBtn;
@property (weak, nonatomic) IBOutlet UITextField *contentTextField;
@property (nonatomic, copy) NSString *contentText;
@end

@implementation TKEditPersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];

}

- (void)setupUI {
    self.navigationItem.title = @"编辑内容";
    self.verificationBtn.backgroundColor = [BLColorConfig mainColor];
    self.verificationBtn.layer.cornerRadius = 15;
    self.verificationBtn.layer.masksToBounds = true;
    self.verificationBtn.enabled = NO;
    self.verificationBtn.alpha = 0.6f;
    
    [self ks_setupLeftBackBarButtonItemWithColor:nil];
    self.contentTextField.tintColor = [BLColorConfig mainColor];
    self.contentTextField.textColor = [BLColorConfig textColor];
    [self.contentTextField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    self.contentTextField.delegate = self;
    self.view.backgroundColor = [UIColor whiteColor];
}

- (IBAction)verificationAcion:(id)sender {
    //回调数据
    if (_contentBlock) {
        self.contentBlock(self.contentTextField.text);
    }
    [KSNavi pop];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)textFieldEditChanged:(UITextField *)textField
{
    NSLog(@"textfield EditChanged text %@",textField.text);
    NSString  *nsTextContent = textField.text;
    NSInteger existTextNum = nsTextContent.length;
    if (existTextNum > 0) {
        self.verificationBtn.enabled = YES;
        self.verificationBtn.alpha = 1.0f;
    } else {
        self.verificationBtn.enabled = NO;
        self.verificationBtn.alpha = 0.6f;
    }
    
    if (existTextNum > MAX_LIMIT_NUMS)
    {
        //截取到最大位置的字符
        NSString *s = [nsTextContent substringToIndex:MAX_LIMIT_NUMS];
        
        [textField setText:s];
        [self ks_showToast:@"您输入的文本太长啦"];
    }

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"textfield EndEditing text %@",textField.text);
    return YES;
}

@end
